package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.dto.SysUserRevicerDto;
import net.zigin.nhi.could.waste.service.SysUserHospitalService;
import net.zigin.nhi.could.waste.vo.SysUserRevicerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.SysUserRevicerMapper;
import net.zigin.nhi.could.waste.model.SysUserRevicer;
import net.zigin.nhi.could.waste.service.SysUserRevicerService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-13 22:41:45                                               
##################################################################*/


/**
 * SysUserRevicer服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUserRevicerServiceImpl
 * @date 2021-9-13 22:41:45
 */
@Service
public class SysUserRevicerServiceImpl extends AbstractServiceImpl<SysUserRevicer, String> implements SysUserRevicerService {
    @Autowired
    private SysUserRevicerMapper sysUserRevicerMapper;
    @Autowired
    private SysUserHospitalService sysUserHospitalService;

    @Override
    public BaseMapper<SysUserRevicer, String> getMapper() {
        return sysUserRevicerMapper;
    }

    @Override
    public String saveOrUpdate(SysUserRevicerDto sysUserRevicerDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalService.getHospitalBaseId();
        sysUserRevicerDto.setHospitalBaseId(hospitalBaseId);
        if (sysUserRevicerDto.getIsDefault() == 1) {
            sysUserRevicerMapper.setDefaultZero(hospitalBaseId);
        }
        SysUserRevicer sysUserRevicer = ReflectUtil.copyProperties(sysUserRevicerDto, SysUserRevicer.class);
        if (StringUtil.isNullOrEmpty(sysUserRevicer.getId())) {
            sysUserRevicer.setId(StringUtil.get32UUID());
            sysUserRevicer.setCreateUser(tokenInfo.getLoginName());
            sysUserRevicer.setCreateTime(new Date());
            sysUserRevicer.setModifyTime(new Date());
            sysUserRevicerMapper.insert(sysUserRevicer);
        } else {
            sysUserRevicer.setModifyUser(tokenInfo.getLoginName());
            sysUserRevicer.setModifyTime(new Date());
            sysUserRevicerMapper.updateByPrimaryKeySelective(sysUserRevicer);
        }
        return sysUserRevicer.getId();
    }

    @Override
    public List<SysUserRevicerVo> getList() {
        String hospitalBaseId = sysUserHospitalService.getHospitalBaseId();
        Map<String, Object> paramMap = ParamsMapUtil.getInstance().addParam("hospitalBaseId", hospitalBaseId);
        List<SysUserRevicer> sysUserRevicers = sysUserRevicerMapper.selectListByCondition(paramMap);
        return ReflectUtil.copyProperties(sysUserRevicers, SysUserRevicerVo.class);
    }

    @Override
    public void setDefault(String id) {
        String hospitalBaseId = sysUserHospitalService.getHospitalBaseId();
        sysUserRevicerMapper.setDefaultZero(hospitalBaseId);
        sysUserRevicerMapper.setDefaultOne(id, hospitalBaseId);
    }

    @Override
    public SysUserRevicerVo getDefault() {
        String hospitalBaseId = sysUserHospitalService.getHospitalBaseId();
        SysUserRevicer revicer = sysUserRevicerMapper.getDefault(hospitalBaseId);
        if(revicer == null){
            revicer = sysUserRevicerMapper.getLastestOne(hospitalBaseId);
        }
        return ReflectUtil.copyProperties(revicer, SysUserRevicerVo.class);
    }
}